// packageI/pages/storagePurchase/storagePurchase.js
const app = getApp()
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    imgUrl: applet_park_api_url_imgUrl,
    token: wx.getStorageSync('TOKEN'),
    areaList: [{
      "name": "请选择区域"
    }],
    currentIndex: 0,
    storeList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      type: options.type
    })
    that.getAreas()
    that.getStock()
  },

  getAreas() {
    var that = this
    wx.request({
      url: applet_park_api_url + '/api/warehouse/list',
      method: "GET",
      success: function (res) {
        if (res.data.code == 0) {
          that.data.areaList = that.data.areaList.concat(res.data.data)
          that.setData(that.data)
        }
      }
    })
  },
  changeCurrentIndex(e) {
    var that = this
    that.setData({
      currentIndex: e.detail.value
    })
    that.getStock()
  },
  getStock() {
    var that = this
    var warehouse = ''
    if (that.data.currentIndex != 0) {
      warehouse = that.data.areaList[that.data.currentIndex].id
    }
    wx.request({
      url: applet_park_api_url + '/api/warehouse/consumables',
      method: 'GET',
      data: {
        warehouseId: warehouse
      },
      success: function (res) {
        if (res.data.code == 0) {
          for (var i in res.data.data) {
            res.data.data[i].images = res.data.data[i].images.split(',')[0]
          }
          that.data.storeList = res.data.data
          that.setData(that.data)
        }
      }
    })
  },
  goAdd(e) {
    var that = this
    if (e.currentTarget.dataset.id) {
      wx.navigateTo({
        url: '../../pages/storageAdd/storageAdd?warehouseId=' + e.currentTarget.dataset.id + '&type=' + that.data.type,
      })
    } else {
      wx.navigateTo({
        url: '../../pages/storageNew/storeageNew',
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    that.getStock()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})